package cn.wolfcode.p2p.util;

import cn.wolfcode.p2p.Exception.CustomException;
import cn.wolfcode.p2p.Exception.ErrorCode;

import java.util.regex.Pattern;

public class AssertUtil {
    //创建asserUtil对象
    private  static final AssertUtil asserUtil = new AssertUtil();

    //验证手机格式的正则表达式
    private static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[0-9])|(18[0,0-9]))\\d{8}$";

    //私有化构造器
    private AssertUtil(){

    }


    /*
        断言手机号
     */
    public AssertUtil isMobile(String mobile, ErrorCode errorCode) {
        isNotNull(mobile,errorCode);
        if(!Pattern.matches(REGEX_MOBILE, mobile)){
            throw new CustomException(errorCode);
        }
        return this;
    }
    /*
        字符串比较
     */
    public AssertUtil isEquals(String currentString,String targetString,ErrorCode errorCode){
        if(!currentString.equals(targetString)){
            throw new CustomException(errorCode);
        }
        return this;
    }

    /*
        断言不为空
     */
    public AssertUtil isNotNull(Object object, ErrorCode errorCode) {
        if(object == null) {
            throw new CustomException(errorCode);
        }
        return this;
    }

    /*
        断言字符串长度
     */
    public AssertUtil isLength(int currentLength,int matchLength, ErrorCode errorCode) {
        if(currentLength != matchLength) {
            throw new CustomException(errorCode);
        }
        return this;
    }
    /*
        断言字符串长度范围
     */
    public AssertUtil isLength(int currentLength,int minLength,int maxLength , ErrorCode errorCode) {
        if(currentLength < minLength || currentLength > maxLength) {
            throw new CustomException(errorCode);
        }
        return this;
    }

    /*
        断言 true
     */
    public AssertUtil isFalse(boolean t, ErrorCode errorCode) {
        if(t) {
            throw new CustomException(errorCode);
        }
        return this;
    }

    /*
        断言 短信结果
     */
    public AssertUtil responseResult(String currentString){
        int result = Integer.parseInt(currentString);
        if(result == -1){
            throw new CustomException(ErrorCode.SEND_NO_USER);
        }else if(result == -2){
            throw new CustomException(ErrorCode.SEND_KEY_ERROR);
        }else if(result == -21){
            throw new CustomException(ErrorCode.SEND_KEY_ENCRYPTION_ERROR);
        }else if(result == -3){
            throw new CustomException(ErrorCode.SEND_INSUFFICIENT_QUANTITY);
        }else if(result == -11){
            throw new CustomException(ErrorCode.SEND_DISABLE);
        }else if(result == -14){
            throw new CustomException(ErrorCode.SEND_ILLEGAL_CHARACTER);
        }else if(result == -4){
            throw new CustomException(ErrorCode.SEND_PHONE_FOMAT_ERROR);
        }else if(result == -41){
            throw new CustomException(ErrorCode.SEND_PHONE_ISNULL);
        }else if(result == -42){
            throw new CustomException(ErrorCode.SEND_CONTENT_ISNULL);
        }else if(result == -51){
            throw new CustomException(ErrorCode.SEND_SIGN_ERROR);
        }else if(result == -52){
            throw new CustomException(ErrorCode.SEND_SIGN_TOLONG);
        }else if(result == -6){
            throw new CustomException(ErrorCode.SEND_IP_ERROR);
        }
        return this;
    }

    /**
     * 获取工具对象
     * @return 工具对象
     */
    public static AssertUtil instance(){
        return asserUtil;
    }
}
